package com.qunar.wan.web.controller;

import com.google.common.base.Preconditions;
import com.qunar.wan.common_api.model.Comment;
import com.qunar.wan.common_api.service.CommentService;
import com.qunar.wan.web.controller.common.AbstractController;
import com.qunar.wan.common_api.serializer.RowBounds;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * User: huiming.suo Date: 13-11-28 Time: 上午10:04
 */

@Controller
@RequestMapping("comment")
public class CommentController extends AbstractController {
    @Resource
    private CommentService commentService;

    @RequestMapping("add")
    @ResponseBody
    public Object addComment(@RequestBody Comment comment) {
        long start = System.currentTimeMillis();
        Preconditions.checkNotNull(comment.getContent());
        comment.setContent(comment.getContent());
        comment.setCreateTime(new Timestamp(System.currentTimeMillis()));
        commentService.insertObj(comment);
        logger.info("评论insert成功，comment={}", comment);
        return dataJson(comment, System.currentTimeMillis() - start);
    }

    @RequestMapping("list")
    @ResponseBody
    public Object selectCommentListByPage(@RequestParam(value = "id") int id, @RequestParam(value = "page") int page,
            @RequestParam(defaultValue = "10") int num) {
        Preconditions.checkNotNull(page);
        long start = System.currentTimeMillis();
        RowBounds rowBounds = new RowBounds((page - 1) * num, page * num);
        return dataJson(commentService.selectByActId(id, rowBounds), System.currentTimeMillis() - start).put("pageNum",
                (commentService.selectCount(id) - 1) / num + 1);
    }

}
